Basic Test: Error rate with different wave number and window length 5


In [3]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

In [4]:
% matplotlib inline

I plot the error of the filtered wave. I use the absulte values of the difference between sine wave and median filtered wave and calculate the mean, to get the error. I use a window length of 5 and different sine wave numbers.

Funcitions


In [5]:
def ErrorPlot( wavenumber,windowLength ):
        data = np.fromfunction( lambda x: np.sin((x-windowLength / 2)/128 * 2 * np.pi * waveNumber), (128 + windowLength / 2, ) )    #creating an array with a sine wave
        datafiltered = medianFilter(data, windowLength)  #calculate the filtered wave with the medianFiltered function
        data = data[ windowLength / 2 : - windowLength ] # slice the data array to synchronize both waves
        datafiltered = datafiltered[ : len(data) ]       # cut the filtered wave to the same length as the data wave
        error = ErrorRate(data,datafiltered,windowLength,wavenumber) #calculate the error with the ErrorRate function
        plt.axis([0, y + 1, 0, 1.2])
        plt.xlabel('Wave number', fontsize = 20)
        plt.ylabel('Error rate', fontsize = 20)
        plt.scatter(*error)

In [6]:
def ErrorRate(data,datafiltered,windowLength, wavenumber):
    errorrate = data-datafiltered  #calculate the difference between the sine wave and the filtered wave
    error = [] #creating a list and save the error rate with the matching wavenumber in it 
    errorrate = np.abs(errorrate)
    error.append([wavenumber ,np.mean(errorrate)])# fill the list with the errorrate and corresponding wave number
    error = zip(*error) #zip the error ([1,1],[2,2],[3,3]) = ([1,2,3],[1,2,3])
    return error

In [9]:
def medianFilter( data, windowLength ): 
    if (windowLength < len(data)and data.ndim == 1):
        tempret = np.zeros(len(data)-windowLength+1)  # creating an array where the filtered values will be saved in
        if windowLength % 2 ==0:                      # check if the window length is odd or even because with even window length we get an unsynchrone filtered wave 
            for c in range(0, len(tempret)):
                tempret[c] = np.median( data[ c : c + windowLength +1 ] ) # write the values of the median filtered wave in tempret, calculate the median of all values in the window
            return tempret
        else:
            for c in range(0, len(tempret)):
                tempret[c] = np.median( data[ c : c + windowLength ] )
            return tempret
    else:
         raise ValueError("windowLength must be smaller than len(data) and data must be a 1D array")

Plot


In [10]:
fig = plt.figure()
for y in range (0,40):
    ErrorPlot(y,5)


With a higher wave number the error rate continues to rise. The error rate at wave number 16 and 32 is unexpected.


In [8]:
pp = PdfPages( 'Error of the median filtered sine waves with different wave numbers.pdf')
pp.savefig(fig)
pp.close()

In [ ]: